package com.taotao.service.impl;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.service.IItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangxiaolin@zhonhajt.com
 * @Title: 商品类目接口实现
 * @Package com.taotao.service.impl
 * @Description:
 * @date 2018/10/11 20:59
 */
@Service
public class ItemCatServiceImpl implements IItemCatService {

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public List<EasyUITreeNode> getItemCatList(long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> itemCatList =  itemCatMapper.selectByExample(example);
        List<EasyUITreeNode> nodeList = new ArrayList<>();

        if (itemCatList.size() > 0) {
            for (TbItemCat itemCat : itemCatList) {
                EasyUITreeNode node = new EasyUITreeNode();
                node.setId(itemCat.getId());
                node.setText(itemCat.getName());
                node.setState(itemCat.getIsParent() ? "closed" : "open");
                nodeList.add(node);
            }
        }
        return nodeList;
    }
}
